﻿#include <QByteArray>
#include <iostream>
#include <string>

#include "zmq.hpp"

int main() {
  zmq::context_t context(1);
  zmq::socket_t socket(context, zmq::socket_type::rep);

  socket.bind("tcp://127.0.0.1:5555");

  zmq::message_t request;
  while (true) {
    auto beforeTime = std::chrono::steady_clock::now();

    socket.recv(request, zmq::recv_flags::none);

    auto afterTime = std::chrono::steady_clock::now();
    //毫秒级
    double duration_millsecond =
        std::chrono::duration<double, std::milli>(afterTime - beforeTime)
            .count();
    std::cout << duration_millsecond << "毫秒" << std::endl;

    if (request.empty()) {
      continue;
    }

    //      std::string request_string(static_cast<char *>(request.data()),
    //                                 request.size());
    auto request_string{request.to_string()};
    std::cout << "Received request: " << request_string << std::endl;
    //      std::cout << "Received request" << std::endl;

    //    zmq::message_t reply(request.size());
    //    memcpy(reply.data(), request.data(), request.size());
    socket.send(zmq::buffer("my reply"), zmq::send_flags::none);
  }

  return 0;
}
